/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestStringBuffer.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.lang;

/**
 * java.lang.StringBuffer测试<br/>
 * 
 * StringBuffer是一个字符容器, 适用于频繁修改字符串的场景<br/>
 * StringBuffer底层使用字符数组来实现, 初始容量为16, 容量不足时每次自增长为原来容量的1倍<br/>
 * 
 * StringBuffer Vs. StringBuilder<br/>
 * 相同点: <br/>
 * 都是字符串缓冲类, 用于一致的方法<br/>
 * 
 * 不同点: <br/>
 * 1. StringBuffer是线程安全的, 操作效率低, StringBuilder是非线程安全的, 操作效率高<br>
 * 2. StringBuffer出现在jdk 1.0, StringBuild出现在jdk 1.5
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestStringBuffer {

	/**
	 * 增加方法 - append()方法
	 */
	public static void testAppend() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.append(' ');
		buffer.append('I');
		buffer.append(new char[] { ' ', 'a', 'm' });
		System.out.println("append() - char: " + buffer.toString());

		buffer.append(" in Wuxi.");
		System.out.println("append() - String: " + buffer.toString());

		buffer.append(15);
		buffer.append(1.23F);
		buffer.append(3.4D);
		buffer.append(899L);
		System.out.println("append() - number: " + buffer.toString());

		buffer.append(false);
		buffer.append(true);
		System.out.println("append() - boolean: " + buffer.toString());
	}

	/**
	 * 增加方法 - insert()方法
	 */
	public static void testInsert() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.insert(2, 'W');
		buffer.insert(3, new char[] { 'A', 'B', 'C' });
		buffer.insert(8, "abc");
		buffer.insert(2, 3);
		buffer.insert(3, 2.3F);
		buffer.insert(6, 3.75D);
		buffer.insert(5, 9843L);
		buffer.insert(2, true);

		System.out.println("buffer.insert() = " + buffer.toString());
	}

	/**
	 * 删除方法 - delete()/deleteCharAt()方法
	 */
	public static void testDelete() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.deleteCharAt(1);
		buffer.deleteCharAt(2);
		System.out.println("buffer.deleteCharAt() = " + buffer.toString());

		buffer.delete(1, 5);
		System.out.println("buffer.delete(0, 5) =  " + buffer.toString());
	}

	/**
	 * 查询方法 - indexOf()/lastIndexOf()
	 */
	public static void testFindStr() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		System.out.println("buffer.indexOf(\"is\") = " + buffer.indexOf("is"));
		System.out.println("buffer.indexOf(\"is\", 4) = " + buffer.indexOf("is", 4));
		System.out.println("buffer.lastIndexOf(\"is\") = " + buffer.lastIndexOf("is"));
		System.out.println("buffer.lastIndexOf(\"is\", 1) = " + buffer.lastIndexOf("is", 1));
	}

	/**
	 * 查询方法 - charAt()
	 */
	public static void testCharAt() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");
		System.out.println("buffer.charAt(2) = " + buffer.charAt(2));
	}

	/**
	 * 查询方法 - capacity()
	 */
	public static void testCapacity() {
		System.out.println("empty StringBuffer's capacity = " + new StringBuffer().capacity());

		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");
		System.out.println("buffer.capacity() = " + buffer.capacity());
	}

	/**
	 * 修改方法 - setCharAt()
	 */
	public static void testSetCharAt() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.setCharAt(2, 'a');
		buffer.setCharAt(3, 't');
		System.out.println("buffer.setCharAt() = " + buffer);
	}

	/**
	 * 修改方法 - replace()
	 */
	public static void testReplace() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.replace(3, 8, "replace");
		System.out.println("buffer.replace() = " + buffer.toString());
	}

	/**
	 * 修改方法 - reverse()
	 */
	public static void testReverse() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");
		System.out.println("buffer.reverse() " + buffer.reverse());
	}

	/**
	 * 修改方法 - substring()
	 */
	public static void testSubString() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");
		System.out.println("buffer.substring(2) = " + buffer.substring(2));
		System.out.println("buffer.substring(2, 9) = " + buffer.substring(2, 9));
	}

	/**
	 * 修改方法 - ensureCapacity()
	 */
	public static void testEnsureCapacity() {
		StringBuffer buffer = new StringBuffer("This is a StringBuffer!");

		buffer.ensureCapacity(20);
		System.out.println("buffer.ensureCapacity(20) = " + buffer.capacity());
	}

	public static void main(String[] args) {
		testAppend();
		testInsert();
		testDelete();

		testFindStr();
		testCharAt();
		testCapacity();

		testSetCharAt();
		testReplace();
		testReplace();
		testSubString();
		testEnsureCapacity();
	}
}
